import { lazy } from "react"
import {
    BarsOutlined
} from '@ant-design/icons';

const routes = [
    {
        icon: <BarsOutlined />,
        name: '基础信息',
        path: '/basicInfo',
        permission: 'jcxx',
        children: [
            {
                name: '用户管理',
                path: '/basicInfo/userList',
                permission: 'jcxx_yhgl',
                component: lazy(() => import('@/pages/basicInfo/userList/page'))
            }, {
                name: '商品列表',
                path: '/basicInfo/productList',
                permission: 'jcxx_splb',
                component: lazy(() => import('@/pages/basicInfo/productList/page'))
            }
        ]
    }, {
        icon: <BarsOutlined />,
        name: '内容管理',
        path: '/contentManagement',
        permission: 'nrgl',
        children: [
            {
                name: "文章列表",
                path: "/contentManagement/articles",
                permission: 'nrgl_wzlb',
                component: lazy(() => import("@/pages/contentManagement/articles/page")),
                children: [
                    {
                        hidden: true,
                        name: "文章详情",
                        path: "/contentManagement/articles/:id",
                        permission: 'nrgl_wzxq',
                        component: lazy(() => import("@/pages/contentManagement/articles/[id]/page"))
                    }
                ]
            }
        ]
    }, {
        icon: <BarsOutlined />,
        name: '系统设置',
        path: '/systemConfiguration',
        permission: 'xtsz',
        children: [
            {
                name: "日志管理",
                path: "/systemConfiguration/logs",
                permission: 'xtsz_rzgl',
                component: lazy(() => import("@/pages/systemConfiguration/logs/page"))
            },
        ]
    }
]

export default routes

// 获取当前用户拥有的路由列表（过滤掉当前用户无权限的路由）
export const getPermissionRoutes = (permissions) => {
    return filterRoutes(routes, permissions)
}

// 通过权限过滤 路由
const filterRoutes = (appRoutes, permissions) => {
    const filter = (routes) => {
        const copiedRoutes = []

        for (const route of routes) {
            if ([...permissions, "0"].includes(route.permission || "")) {
                const copiedRoute = {
                    ...route,
                    children: [],
                }

                if (route.children?.length) {
                    copiedRoute.children = filter(route.children)
                }

                copiedRoutes.push(copiedRoute)
            }
        }

        return copiedRoutes
    }

    const filteredRoutes = filter(appRoutes)

    return filteredRoutes
}